package com.css.dzj.zfjg.modules.riskwarning.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.riskwarning.entity.JgLhjgRiskWarningEntity;
import com.css.dzj.zfjg.modules.riskwarning.service.JgLhjgRiskWarningService;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("generator/jglhjgriskwarning")
public class JgLhjgRiskWarningController {

    @Autowired
    private JgLhjgRiskWarningService jgLhjgRiskWarningService;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                  @RequestParam(name = "pageNo" ,required = false)Integer pageNo){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            QueryResults page = jgLhjgRiskWarningService.queryPage(pageRequest);
            baseService.logSuccess(3, "分页查询企业风险预警成功", "2");
            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询企业风险预警失败", "2");
            return R.error(e.getMessage());
        }

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{riskId}")
    public R info(@PathVariable("riskId") String riskId){
        try {
            JgLhjgRiskWarningEntity jgLhjgRiskWarning = jgLhjgRiskWarningService.getById(riskId);
            baseService.logSuccess(3, "通过id获取企业风险预警信息成功", "2");
            return R.ok().put("jgLhjgRiskWarning", jgLhjgRiskWarning);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取企业风险预警信息失败", "2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgRiskWarningEntity jgLhjgRiskWarning){
        try {
            jgLhjgRiskWarningService.save(jgLhjgRiskWarning);
            baseService.logSuccess(3, "企业风险预警信息添加成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业风险预警信息添加失败", "2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgRiskWarningEntity jgLhjgRiskWarning){
        try {
            JgLhjgRiskWarningEntity grw = jgLhjgRiskWarningService.getById(jgLhjgRiskWarning.getRiskId());
            BeanUtil.copyBeanNotNullToBean(jgLhjgRiskWarning, grw);
            baseService.logSuccess(3, "企业风险预警信息修改成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业风险预警信息修改失败", "2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] riskIds){
        try {
            List list = Arrays.asList(riskIds);
            jgLhjgRiskWarningService.removeByIds(list);
            baseService.logSuccess(3, "企业风险预警信息删除成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业风险预警信息删除失败", "2");
            return R.error(e.getMessage());
        }

    }

}
